package com.dxy.yzs.marathon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxy.yzs.marathon.common.R;
import com.dxy.yzs.marathon.domain.Label;
import com.dxy.yzs.marathon.domain.UserLabelRelation;
import com.dxy.yzs.marathon.service.LabelService;
import com.dxy.yzs.marathon.service.UserLabelRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 讨论组
 */
@RestController
@RequestMapping("group")
public class DiscussionGroupController {
    @Autowired
    private UserLabelRelationService userLabelRelationService;
    @Autowired
    private LabelService labelService;

    /**
     * 加入讨论组
     * @return
     */
    @PostMapping("joinDiscussionGroup")
    public R joinDiscussionGroup(@RequestBody UserLabelRelation userLabelRelation){
        userLabelRelationService.save(userLabelRelation);
        return R.ok();
    }

    /**
     * 获取当前用户加入过的讨论组
     * @return
     */
    @GetMapping("getDiscussionGroupByUserId")
    public R getDiscussionGroupByUserId(Integer userId){
        List<UserLabelRelation> list = userLabelRelationService.list(
                new QueryWrapper<UserLabelRelation>()
                        .eq("user_id", userId)
        );
        List<Integer> labelIds = list.stream().map(UserLabelRelation::getLabelId).collect(Collectors.toList());
        List<Label> labels = labelService.listByIds(labelIds);
        return R.ok().put("data",labels);
    }
}
